﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DreamIn.Structures.Utils
{
    /// <summary>
    /// Custom List<T> for threading."/>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public sealed class ThreadSafeList<T> 
    {
        private List<T> m_list = new List<T>();
        private object m_lock = new object();
        /// <summary>
        /// Add object.
        /// </summary>
        /// <param name="value"></param>
        public void Add(T value)
        {
            lock (m_lock)
            {
                m_list.Add(value);
            }
        }
        /// <summary>
        /// Remove object.
        /// </summary>
        /// <param name="value"></param>
        public void Remove(T value)
        {
            lock (m_lock)
            {
                m_list.Remove(value);
            }
        }
        /// <summary>
        /// Return if list contains object.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Contains(T value)
        {
            lock (m_lock)
            {
                return m_list.Contains(value);
            }
        }
        /// <summary>
        /// List count.
        /// </summary>
        public int Count { get { lock (m_lock) { return m_list.Count; } } }
        /// <summary>
        /// Get object using index.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T this[int index]
        {
            get { lock (m_lock) { return m_list[index]; } }
            set { lock (m_lock) { m_list[index] = value; } }
        }
        public List<T> GetList()
        {
            lock (m_lock)
            {
                return m_list;
            }
        }
    }
}